package com.openatc.autoprogram.thread;

import com.google.gson.Gson;
import com.openatc.autoprogram.model.MessageData;
import com.openatc.autoprogram.model.StatusPattern;
import com.openatc.autoprogram.service.MsgService;
import com.openatc.autoprogram.utils.ApplicationContextProvider;

import java.util.Random;

/**
 * @author ：panleilei
 * @description
 * @modified By：
 * @version:
 * @date ：Created in 2023/3/20 17:50
 */
public class AutoSendStatusPattern extends Thread {
    private MsgService msgService = ApplicationContextProvider.getBean(MsgService.class);
    private static final String REPORT = "report";
    private static final String STATUS_PATTERN = "status/pattern";

    private String agentid;
    private Gson gson = new Gson();

    public AutoSendStatusPattern(String ip, int port,String agentid){
        this.agentid = agentid;
    }


    @Override
    public void run() {
        sendStatusPatternMsg();

        Random rand = new Random();
        int randint = rand.nextInt(900)+ 100;// 三位随机整数

        try {
            Thread.sleep(30 * randint);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


    private void sendStatusPatternMsg() {

        MessageData msg = new MessageData();
        msg.setAgentid(agentid);
        msg.setThirdpartyid(agentid);
        msg.setOperation(REPORT);
        msg.setInfotype(STATUS_PATTERN);

        StatusPattern statusPattern = new StatusPattern();
        statusPattern.setControl(5);
        statusPattern.setMode(0);

        msg.setData(gson.toJsonTree(statusPattern));

        msgService.sendUdpMsgToOpenATC(msg);

    }
}
